package View;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import Model.Map;
import Model.Pedestrian;
import Model.Player;

import javax.imageio.ImageIO;

/**
 * Created by anton.kasiyanenko@stud.fh-luebeck.de on 21.10.2014.
 */
public class MapRenderer implements Renderer {

    private Map map;
    private Image pedestrianImage               = null;
    private boolean bool_collision              = false;
    private boolean bool_destination_reached    = false;

    public MapRenderer(Map map) {
        this.map = map;

        try {
            pedestrianImage = ImageIO.read(new File("resources/image/dummy-happy-icon.png"));
        } catch (IOException e) {
            System.out.println("image not found");
        }
    }

    /**
     * Gibt an ob der Spieler mit einem Fußgänger zusammengestoßen ist.
     * @return Gab es eine Kollision
     */
    public boolean has_collision(){
        return bool_collision;
    }

    /**
     * Der Status, dass es eine Kollision gab wird zurückgesetzt
     */
    public void unset_collision(){
        bool_collision = false;
    }

    /**
     * Gibt an ob der Spieler das andere Ende der Fußgängerzone erreicht hat.
     * @return Ziel erreicht
     */
    public boolean is_dest_reached(){
        return bool_destination_reached;
    }

    /**
     * Der Status, dass der Spieler das Ziel erreicht hat wird zurückgesetzt
     */
    public void unset_dest_reached(){
        bool_destination_reached = false;
    }

    @Override
    public void render(Graphics g, int level, int score) {

        // spieler zeichnen
        g.setColor(new Color(0, 1f, 0));
        Player player = map.getPlayer();

        int x = (int) (player.getX() * map.cellSize);
        int y = (int) (player.getY() * map.cellSize);

        g.fillOval(x + 2, y + 2, map.cellSize - 4, map.cellSize - 4);

        // fussgaenger zeichnen
        g.setColor(new Color(0.2901961f, 0.5803922f, 0.7019608f));
        for (Pedestrian pdstr : map.getPedestrians()) {
            int x_abs_ped = (int) pdstr.getX();
            int y_abs_ped = (int) pdstr.getY();
            int x_pedestrian = (int) (x_abs_ped * map.cellSize +2);
            int y_pedestrian = (int) (y_abs_ped * map.cellSize +2);

            g.drawImage(pedestrianImage, x_pedestrian, y_pedestrian, map.cellSize, map.cellSize, null);
            if( (x_abs_ped == player.getX()) && (y_abs_ped == player.getY()) )
            {
                // Kollision!
                bool_collision = true;
            }
            else if ((12 == player.getX()) && (3 == player.getY())){
                bool_destination_reached = true;
            }
        }

        g.setColor(Color.WHITE);
        g.setFont(new Font("TimesRoman", Font.BOLD, 24));
        g.drawString("Level: " + level + "      Score: " + score, 10, 24);
    }
}
